package com.jfs.security.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jfs.result.Result;
import com.jfs.security.annotations.HasAuthority;
import com.jfs.security.annotations.Log;
import com.jfs.security.annotations.LoginUser;
import com.jfs.security.dto.DropUserDto;
import com.jfs.security.dto.UserDto;
import com.jfs.security.service.UserService;
import com.jfs.security.vo.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Description 用户控制器
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/8/9
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {
    private final UserService userService;

    @Log(name = "获取用户列表")
    @GetMapping
    @HasAuthority(HasAuthority.HasAuthorityEnum.Read)
    public Result<IPage<UserDto>> list(UserListVo userListVo) {
        return Result.success("查询用户列表成功", this.userService.list(userListVo));
    }

    @Log(name = "添加用户")
    @PostMapping
    @HasAuthority(HasAuthority.HasAuthorityEnum.ReadAndWrite)
    public Result<Object> add(@RequestBody @Valid AddUserVo addUserVo) {
        return this.userService.save(addUserVo);
    }

    @Log(name = "修改用户信息")
    @PutMapping
    @HasAuthority(HasAuthority.HasAuthorityEnum.ReadAndWrite)
    public Result<Object> update(@RequestBody @Valid UpdateUserVo updateUserVo) {
        return this.userService.update(updateUserVo);
    }

    @Log(name = "禁用用户")
    @PutMapping("/disable")
    @HasAuthority(HasAuthority.HasAuthorityEnum.ReadAndWrite)
    public Result<Object> disable(@RequestBody @Valid DisableUserVo disableUserVo) {
        return this.userService.disable(disableUserVo);
    }
    @Log(name = "启用用户")
    @PutMapping("/enable")
    @HasAuthority(HasAuthority.HasAuthorityEnum.ReadAndWrite)
    public Result<Object> enable(@RequestBody @Valid DisableUserVo disableUserVo) {
        return this.userService.enable(disableUserVo);
    }

    @Log(name = "删除用户")
    @DeleteMapping
    @HasAuthority(HasAuthority.HasAuthorityEnum.ReadAndWrite)
    public Result<Object> delete(@RequestBody @Valid DisableUserVo disableUserVo) {
        return this.userService.delete(disableUserVo);
    }

    @Log(name = "用户修改密码")
    @PutMapping("/password")
    @HasAuthority(HasAuthority.HasAuthorityEnum.Read)
    public Result<Object> updatePassword(@LoginUser LoginUserEntity loginUserEntity, @RequestBody @Valid UpdateUserPasswordVo updateUserPasswordVo) {
        return this.userService.updatePassword(loginUserEntity, updateUserPasswordVo);
    }

    @Log(name = "退出登录")
    @DeleteMapping("/logout")
    @HasAuthority(HasAuthority.HasAuthorityEnum.Read)
    public Result<Object> logOut(@LoginUser LoginUserEntity loginUserEntity) {
        return this.userService.logOut(loginUserEntity);
    }

    @Log(name = "获取当前登录用户信息")
    @GetMapping("/info")
    @HasAuthority(HasAuthority.HasAuthorityEnum.Read)
    public Result<Object> info(@LoginUser LoginUserEntity loginUserEntity) {
        return this.userService.info(loginUserEntity);
    }

    /**
     * 获取用户下拉列表
     * @return
     */
    @GetMapping("/drop")
    @HasAuthority(HasAuthority.HasAuthorityEnum.Read)
    public Result<List<DropUserDto>> drop(){
        return Result.success("查询用户下拉列表成功",this.userService.drop());
    }
}
